package org.darcy.controller.base;

import java.util.List;

import org.darcy.controller.BaseController;
import org.darcy.entity.base.BaseDictionary;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.R;
import org.darcy.service.base.BaseDictionaryService;
import org.darcy.utils.common.ReturnCode;
import org.darcy.utils.vo.ReturnVo;
import org.darcy.vo.CheckExistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "BaseDictionary接口：数据字典")
@RestController
@RequestMapping("/base/dictionary")
@Slf4j
public class BaseDictionaryController extends BaseController<BaseDictionary> {

	private final String info_prefix = "[数据字典模块]";

	@Autowired
	private BaseDictionaryService dictionaryService;

	/**
	 * 判断字段是否存在
	 *
	 * @param checkExistVo 参数
	 * @return
	 */
	@PostMapping(value = "/checkEntityExist", produces = "application/json")
	protected R checkEntityExist(@RequestBody CheckExistVo checkExistVo) {
		return this.checkExist(dictionaryService, checkExistVo);
	}

	@Operation(summary = "数据字典分页列表")
	@PostMapping("list")
	public R list(@RequestBody PageParam<BaseDictionary> param) {
		log.debug("{}-列表数据", info_prefix);
		PageData<BaseDictionary> data = dictionaryService.page(param);
		return R.ok().data(data);
	}

	@Operation(summary = "数据字典详情")
	@GetMapping("/view/{id}")
	public R view(@PathVariable Integer id) {
		log.debug("{}-查看数据字典信息", info_prefix);
		BaseDictionary data = dictionaryService.getById(id);
		return R.ok().data(data);
	}

	@Operation(summary = "数据字典添加或者修改")
	@PostMapping("saveOrUpdate")
	public ReturnVo<String> saveOrUpdate(@RequestBody BaseDictionary dictionary) {
		ReturnVo<String> returnVo = new ReturnVo<>(ReturnCode.FAIL, "保存失败");
		try {
			if (dictionary.getId() == null) {
				dictionaryService.insert(dictionary);
			} else {
				dictionaryService.update(dictionary);
			}
			returnVo.setMsg("保存成功！");
			returnVo.setCode(ReturnCode.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			returnVo.setMsg(e.getMessage());
		}
		return returnVo;
	}

	@Operation(summary = "根据ID删除数据字典")
	@PostMapping("/del/{id}")
	public R del(@PathVariable String id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		dictionaryService.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "传入字典类型名称，返回对应的字典列表")
	@GetMapping("getList/{typeName}")
	public R getList(@PathVariable String typeName) {
		List<BaseDictionary> data = dictionaryService.getList(typeName);
		return R.ok().data(data);
	}

	@Operation(summary = "委托业务列表")
	@GetMapping("project_type")
	public R project_type() {
		List<BaseDictionary> data = dictionaryService.getList("委托业务");
		return R.ok().data(data);
	}

	@Operation(summary = "检测类别列表")
	@GetMapping("check_type")
	public R check_type() {
		List<BaseDictionary> data = dictionaryService.getList("检测类别");
		return R.ok().data(data);
	}

}
